package com.china08.yxyapi.api.home.kecheng.cloud.ques;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.service.manage.tec.teaching.lecture.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 
 * 云课程--查询课程对应试题
 *
 */
@RestController
@CrossOrigin
public class KechengCloudQuesSchController extends YxyApiBaseController {

    @Autowired
    private ElasticQuesService elasticQuesService;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/ques/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel)
                    throws JsonParseException, JsonMappingException, IOException {

        List<String> quesId = reqModel.quesIds;
        List<ElasticYxyYxyQuesResource> quesIdIn = elasticQuesService.findByQuesIdIn(quesId);
        List<QuesModel> mulQuesList = new ArrayList<QuesModel>();
        List<QuesModel> gapQuesList = new ArrayList<QuesModel>();
        List<QuesModel> ansQuesList = new ArrayList<QuesModel>();
        List<QuesModel> sinQuesList = new ArrayList<QuesModel>();
        QuesModel quesModel = null;
        for (ElasticYxyYxyQuesResource eQues : quesIdIn) {
            String questionType = eQues.getQuestionType();
            if (StringUtils.equals(questionType, "8")) {
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                quesModel.questionType = eQues.getQuestionType();
                // 获取多选题
                mulQuesList.add(quesModel);
            } else if (StringUtils.equals(questionType, "1")) {
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                quesModel.questionType = eQues.getQuestionType();
                // 获取单选题
                sinQuesList.add(quesModel);
            } else if (StringUtils.equals(questionType, "2")) {
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                quesModel.questionType = eQues.getQuestionType();
                // 获取填空题
                gapQuesList.add(quesModel);
            } else {
                quesModel = new QuesModel();
                quesModel.quesId = eQues.getId();
                quesModel.quesBody = eQues.getQuesBody();
                quesModel.difficultyLevel = eQues.getDifficultyLevel();
                quesModel.knowledgeTexts = eQues.getKnowledgeTexts();
                quesModel.questionType = eQues.getQuestionType();
                // 获取解答题
                ansQuesList.add(quesModel);

            }
        }

        RespModel respModel = new RespModel();
        respModel.ansQuesList = ansQuesList;
        respModel.gapQuesList = gapQuesList;
        respModel.mulQuesList = mulQuesList;
        respModel.sinQuesList = sinQuesList;
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 试题ID
        public List<String> quesIds;

    }

    public static class QuesModel {

        // 题id
        public String quesId;

        // 题干
        public Object quesBody;

        // 题型难度
        public String difficultyLevel;

        // 试题涉及的知识点
        public List<String> knowledgeTexts;

        public String questionType;

    }

    protected static class RespModel {

        // 多选题
        public List<QuesModel> mulQuesList;
        // 单选题
        public List<QuesModel> sinQuesList;
        // 填空题
        public List<QuesModel> gapQuesList;

        // 解答题
        public List<QuesModel> ansQuesList;

    }
}
